package dhm.com.hpx.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import dhm.com.hpx.R;
import dhm.com.hpx.entity.AddextensionBean;
import dhm.com.hpx.utils.EmojiFilter;

/**
 * Created by admin
 * 2019/4/29
 * 所有订单适配器
 */
public class AddextensionAdapter extends RecyclerView.Adapter<AddextensionAdapter.ViewHolder> {

    private Context context;

    List<String> platformLiat = new ArrayList();
    List<AddextensionBean> list = new ArrayList<>();

    public AddextensionAdapter(Context context) {
        this.context = context;
        platformLiat.clear();
        platformLiat.add("抖音");
        platformLiat.add("快手");
        platformLiat.add("淘宝");
        platformLiat.add("阿里");
        platformLiat.add("咸鱼");
        platformLiat.add("京东");
        platformLiat.add("飞猪");
        platformLiat.add("火山 视频（极速版）");
        platformLiat.add("拼多多");
        platformLiat.add("饿了么");
        platformLiat.add("阿里巴巴");
        platformLiat.add("美团");


    }

    public void setList(List<AddextensionBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ViewHolder viewHolder;
        View view = LayoutInflater.from(context).inflate(R.layout.item_addextension, null);
        viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {

        viewHolder.edit_command.setFilters(new InputFilter[]{new EmojiFilter(), new InputFilter.LengthFilter(400)});
        if (list.get(i).getPlatform()>0){
            viewHolder.edit_platform.setText(platformLiat.get(list.get(i).getPlatform()-1));
        }

        if (list.get(i).getCommand() != null && !list.get(i).getCommand().equals("")) {
            viewHolder.edit_command.setText(list.get(i).getCommand());
        }
        viewHolder.edit_command.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                onCommandClick.item(i, viewHolder.edit_command.getText().toString());
            }
        });

        viewHolder.edit_platform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onPlatformClick.item(i);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        //        public SimpleDraweeView image;
//        public EditText edit_title;
//        public EditText edit_desc;
//        public EditText edit_link;
        public TextView edit_platform;
        public EditText edit_command;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
//            image=itemView.findViewById(R.id.image);
//            edit_title=itemView.findViewById(R.id.edit_title);
//            edit_desc=itemView.findViewById(R.id.edit_desc);
//            edit_link=itemView.findViewById(R.id.edit_link);
            edit_platform = itemView.findViewById(R.id.edit_platform);
            edit_command = itemView.findViewById(R.id.edit_command);
        }
    }


    public interface OnPlatformClick {
        void item(int posttion);
    }

    private OnPlatformClick onPlatformClick;

    public void onTitleClick(OnPlatformClick onClick) {
        this.onPlatformClick = onClick;
    }

    public interface onCommandClick {
        void item(int posttion, String value);
    }

    private onCommandClick onCommandClick;

    public void onCommandClick(onCommandClick onClick) {
        this.onCommandClick = onClick;
    }
}

